package com.cuit.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.cuit.mapper.UserEditMapper;
import com.cuit.pojo.User;
import com.cuit.result.R;
import com.cuit.service.FileService;
import com.cuit.utils.HashUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Api(tags = "用户信息设置")
@CrossOrigin
@RestController
@RequestMapping("/user_edit")
public class UserEditController {

    @Autowired
    private FileService fileService;

    @Autowired
    private UserEditMapper userEditMapper;

    //根据ID获取用户信息
    @GetMapping("/user/{id}")
    @ApiOperation("根据ID获取用户信息")
    public User getUserById(@PathVariable String id){
        User user = userEditMapper.selectById(id);
        user.setAvatar(fileService.allPath(user.getAvatar()));
        return user;
    }

    //修改用户信息
    @RequestMapping("/user/edit")
    @ApiOperation("修改用户信息")
    public int editByQuery(@RequestParam("user") String userStr, @RequestParam("file")MultipartFile file){
        User user = JSON.parseObject(userStr,User.class);
            try {
                fileService.deleteImg(user.getAvatar());
                String fileStr = fileService.getImgPathSQL(file);
                user.setAvatar(fileStr);
            } catch (IOException e) {
                e.printStackTrace();
            }

            int i = userEditMapper.updateById(user);
            System.out.println(i);
            return 0;
    }

    //修改用户信息(无头像修改)
    @RequestMapping("/user/edit-noFile")
    @ApiOperation("修改用户信息(无头修改)")
    public int editByQueryWithOutFile(@RequestParam("user") String userStr){
        User user = JSON.parseObject(userStr,User.class);
        String avatar = user.getAvatar();
        String SQLAvatar = avatar.substring(23);
        user.setAvatar(SQLAvatar);
        int i = userEditMapper.updateById(user);
        System.out.println(i);
        return 0;
    }




    //修改用户密码
    @RequestMapping("/password_changing/{o_password}&{user_id}&{n_password}")
    @ApiOperation("修改用户密码")
    public R password_changing(@PathVariable String o_password,@PathVariable String user_id, @PathVariable String n_password){
        User user = userEditMapper.selectById(user_id);
         o_password = HashUtils.encryPassword(o_password);
        if(!o_password.equals(user.getPassword())){
            return R.error().setMessage("原密码输入不正确!");
        }
        user.setPassword(HashUtils.encryPassword(n_password));
        userEditMapper.updateById(user);
        return R.ok();
    }

    //注销账号
    @RequestMapping("/account_cancellation/{user_id}")
    @ApiOperation("注销账号")
    public R account_cancellation(@PathVariable String user_id){
        userEditMapper.deleteById(user_id);
        return R.ok();
    }
}
